﻿
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Auto_Buy.Component
{
    public class ProductBuy
    {
        public static async Task<BuyResult> Buy(long productId, int share, string tradePwd, string token)
        {
            var body = new RequestBody();
            body.Method = "Buy";
            body.Token = token;
            var d = JsonConvert.SerializeObject(new { productId, share, TradingPassword = tradePwd });
            body.Data = d;
            var respone = await HttpUnit.Post(body);
            if (respone.IsSuccessStatusCode)
            {
                var res = await respone.Content.ReadAsStringAsync();
                Console.WriteLine(res);
                var obj = JsonConvert.DeserializeObject<dynamic>(res);
                BuyResult data = JsonConvert.DeserializeObject<BuyResult>(obj.d.ToString());
                data.Code = obj.s.ToString();
                data.Message = obj.es.ToString();
                return data;
            }
            return new BuyResult();
        }
    }
}
